/* axios配置文件 */
import axios from 'axios'
import qs from 'qs'

const Axios = axios.create({
  baseURL: '', //配置接口地址
  timeout: 10000,
  responseType: 'json',
  withCredentials: false, // `withCredentials` 表示跨域请求时是否需要使用凭证
  headers: { //配置请求头
    // "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
    'Content-Type': 'application/json'
  }
});


// POST传参序列化
Axios.interceptors.request.use((config) => {
  if (config.method === 'post' || config.method === "put" || config.method === "delete") {
    config.data = qs.stringify(config.data); //一个JavaScript值(对象或者数组)转换为一个 JSON字符串
  }
  // 若是有做鉴权token , 就给头部带上token
  /* if (localStorage.token) {
    config.headers.Authorization = localStorage.token;
  } */
  return config;
}, (error) => {
  // console.log("错误的传参:" + error);
  return Promise.reject(error);
});

// code状态码200判断
Axios.interceptors.response.use((res) => {
  if(res.status != '200'){
    console.log(res);
    return Promise.reject(res);
  }
  return res;
}, (error) => {
  // console.log("网络异常:"+error);
  return Promise.reject(error);
});

export default {
  install: function (Vue, Option) {
    Object.defineProperty(Vue.prototype, "$http", {
      value: Axios
    });
  }
};
